package esercitazione5.JUnitTest;

import java.util.ArrayList;

import org.junit.*;

import static org.junit.Assert.*;
import esercitazione5.graphs.Graph;
import esercitazione5.graphs.GraphInterface;
import esercitazione5.graphs.Vertex;
import esercitazione5.graphs.Visit;

public class VisitTest {

	Visit<Integer, Integer> v;
	GraphInterface<Integer, Integer> g;
	Vertex<Integer> due;
	Vertex<Integer> tre;
	
	
	@Before
	public void setUp(){
		g = new Graph<Integer, Integer>();
		Vertex<Integer> uno = g.insertVertex(1);
		due = g.insertVertex(2);
		tre = g.insertVertex(3);
		Vertex<Integer> quattro = g.insertVertex(4);
		Vertex<Integer> cinque = g.insertVertex(5);
		Vertex<Integer> sei = g.insertVertex(6);
		Vertex<Integer> sette = g.insertVertex(7);
		Vertex<Integer> otto = g.insertVertex(8);
		Vertex<Integer> nove = g.insertVertex(9);
		Vertex<Integer> dieci = g.insertVertex(10);
		Vertex<Integer> undici = g.insertVertex(11);

		g.insertEdge(due, tre, 1);
		g.insertEdge(due, cinque, 1);
		g.insertEdge(quattro, cinque, 1);
		g.insertEdge(quattro, dieci, 1);
		g.insertEdge(cinque, sette, 4);
		g.insertEdge(sette, uno, 1);
		g.insertEdge(sette, nove, 1);
		g.insertEdge(uno, undici, 1);
		g.insertEdge(sei, otto, 1);
		g.insertEdge(dieci, sette, 1);
		v = new Visit<>(g);
	}
	
	@Test
	public void testBFS(){
		ArrayList<Vertex<Integer>> l1 =  (ArrayList<Vertex<Integer>>) v.bfs(due);
		assertEquals(11, l1.size());
		
		ArrayList<Vertex<Integer>> l2 =  (ArrayList<Vertex<Integer>>) v.bfs(tre);
		assertEquals(11, l2.size());
	}
	
	@Test
	public void testDFS(){
		ArrayList<Vertex<Integer>> l1 =  (ArrayList<Vertex<Integer>>) v.dfs(due,0);
		assertEquals(11, l1.size());
		assertEquals(0,l1.get(0).getProperty("apertureTime"));
		assertEquals(g.numVertices()*2-1,l1.get(l1.size()-1).getProperty("finishTime"));
		
		ArrayList<Vertex<Integer>> l2 =  (ArrayList<Vertex<Integer>>) v.dfs(tre,0);
		assertEquals(11, l2.size());
		assertEquals(0,l2.get(0).getProperty("apertureTime"));
		assertEquals(g.numVertices()*2-1,l2.get(l2.size()-1).getProperty("finishTime"));
	}
	
	@Test
	public void testTopologicalOrder(){
		ArrayList<Vertex<Integer>> l1 =  (ArrayList<Vertex<Integer>>) v.topologicalOrder(due);
		assertEquals(11, l1.size());
		assertEquals(new Integer(3),l1.get(l1.size()-1).getValue());
		
		ArrayList<Vertex<Integer>> l2 =  (ArrayList<Vertex<Integer>>) v.topologicalOrder(tre);
		assertEquals(11, l2.size());
		assertEquals(new Integer(3),l2.get(l2.size()-1).getValue());
	}
}
